#import "ZQWordCell.h"
#import "SDAutoLayout.h"
#import "ZQWordPage.h"
#import <MJExtension/MJExtension.h>
#import "ZQBaseTablePage.h"
#import "AFNetworkingHelper.h"
#import "UIHelper.h"
#import "ZQBaseModel.h"
#import "ZQBaseCell.h"
#import "ZQTWordModels.h"
#import <MJRefresh/MJRefresh.h>
static NSString * const ID = @"cell";
@interface ZQWordPage ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic , weak) UITableView *tableView;
@property (nonatomic , assign) NSInteger page;
@property (nonatomic , strong) NSMutableArray <ZQTWordModels *> *topics;
@property (nonatomic , strong) ZQTWordModels *model;
@end
@implementation ZQWordPage
- (UITableView *)tableView
{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        [self.view addSubview:tableView];
        _tableView = tableView;
    }
    return _tableView;
}
- (NSMutableArray<ZQTWordModels *> *)topics
{
    if (!_topics) {
        _topics = [NSMutableArray array];
    }
    return _topics;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = ZQRandomColor;
    self.navigationItem.title =  @"欢乐天地";
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ZQWordCell class]) bundle:nil] forCellReuseIdentifier:ID];
    MJRefreshHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewTopics)];
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopics)];
}
#pragma mark - === loadMoreTopics ===
- (void)loadMoreTopics
{
    self.page ++;
    AFHTTPSessionManager *mgf = [AFHTTPSessionManager manager];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"sort"] = @"desc";
    params[@"page"] = @(self.page);
    params[@"pagesize"] = @(10);
    params[@"time"] = @"1418816972";
    params[@"key"] = @"e53f4d128405ec557c31950fb3c87a4a";
    [mgf GET:@"http://v.juhe.cn/joke/content/list.php" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary   * _Nullable responseObject) {
        [self.topics addObjectsFromArray: [ZQTWordModels mj_objectArrayWithKeyValuesArray:responseObject[@"result"][@"data"]]];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [UIHelper showErrorWithText:@"网络繁忙,稍后在试" inView:self.view];
    }];
}
- (void)loadNewTopics
{
    self.page = 1;
    AFHTTPSessionManager *mgf = [AFHTTPSessionManager manager];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"sort"] = @"desc";
    params[@"page"] = @(self.page);
    params[@"pagesize"] = @(10);
    params[@"time"] = @"1418816972";
    params[@"key"] = @"e53f4d128405ec557c31950fb3c87a4a";
    [mgf GET:@"http://v.juhe.cn/joke/content/list.php" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary   * _Nullable responseObject) {
        self.topics = [ZQTWordModels mj_objectArrayWithKeyValuesArray:responseObject[@"result"][@"data"]];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [UIHelper showErrorWithText:@"网络繁忙,稍后在试" inView:self.view];
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.topics.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZQWordCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.zqwordModel = self.topics[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZQTWordModels *model = self.topics[indexPath.row];
   return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"zqwordModel" cellClass:[ZQWordCell class] contentViewWidth:[self cellContentViewWith]];
}
- (CGFloat)cellContentViewWith
{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    if ([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationPortrait && [[UIDevice currentDevice].systemVersion floatValue] < 8) {
        width = [UIScreen mainScreen].bounds.size.height;
    }
    return width;
}
@end
